﻿using System;
using LRS.DTO;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace LRS.DAL
{
    public static class dCustomer
    {
        public static List<eCustomer> GetCustomers()
        {
            List<eCustomer> list = new List<eCustomer>();
            SqlDataReader reader = SqlHelper.ExecuteReader("Select * from Customer");
            while (reader.Read())
            {
                eCustomer customer = new eCustomer();
                customer.CustomerID = Int32.Parse(reader["CustomerID"].ToString());
                customer.FirstName = reader["FirstName"].ToString();
                customer.LastName = reader["LastName"].ToString();
                customer.Gender = Boolean.Parse(reader["Gender"].ToString());
                customer.HometownAddress = reader["HometownAddress"].ToString();
                customer.Phone = reader["Phone"].ToString();
                customer.SocialID = reader["SocialID"].ToString();
                customer.CurrentAddress = reader["CurrentAddress"].ToString();
                customer.DateOfBirth = DateTime.Parse(reader["DateOfBirth"].ToString());
                customer.Email = reader["Email"].ToString();
                customer.CustomerNumber = reader["CustomerNumber"].ToString();
                list.Add(customer);
            }
            reader.Close();
            return list;
        }

        public static List<eCustomer> GetCustomersByKeyword(string keyword)
        {
            List<eCustomer> list = new List<eCustomer>();
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@keyword", keyword));
            SqlDataReader reader = SqlHelper.ExecuteReader("Select * from Customer c where c.CustomerNumber like '%' + @keyword +'%' OR c.FirstName like '%' + @keyword +'%' OR c.LastName like '%' + @keyword +'%'",parameters);
            while (reader.Read())
            {
                eCustomer customer = new eCustomer();
                customer.CustomerID = Int32.Parse(reader["CustomerID"].ToString());
                customer.FirstName = reader["FirstName"].ToString();
                customer.LastName = reader["LastName"].ToString();
                customer.Gender = Boolean.Parse(reader["Gender"].ToString());
                customer.HometownAddress = reader["HometownAddress"].ToString();
                customer.Phone = reader["Phone"].ToString();
                customer.SocialID = reader["SocialID"].ToString();
                customer.CurrentAddress = reader["CurrentAddress"].ToString();
                customer.DateOfBirth = DateTime.Parse(reader["DateOfBirth"].ToString());
                customer.Email = reader["Email"].ToString();
                customer.CustomerNumber = reader["CustomerNumber"].ToString();
                list.Add(customer);
            }
            reader.Close();
            return list;
        }
        public static eCustomer GetCustomerByID(int CustomerID)
        {
            SqlDataReader reader = SqlHelper.ExecuteReader("Select * from Customer where CustomerID=" + CustomerID.ToString());
            reader.Read();
            eCustomer customer = new eCustomer();
            customer.FirstName = reader["FirstName"].ToString();
            customer.LastName = reader["LastName"].ToString();
            customer.Gender = Boolean.Parse(reader["Gender"].ToString());
            customer.HometownAddress = reader["HometownAddress"].ToString();
            customer.Phone = reader["Phone"].ToString();
            customer.SocialID = reader["SocialID"].ToString();
            customer.CurrentAddress = reader["CurrentAddress"].ToString();
            customer.DateOfBirth = DateTime.Parse(reader["DateOfBirth"].ToString());
            customer.Email = reader["Email"].ToString();
            customer.CustomerNumber = reader["CustomerNumber"].ToString();
            customer.CustomerID = Int32.Parse(reader["CustomerID"].ToString());
            reader.Close();
            return customer;
        }
        public static void UpdateCustomer(eCustomer customer)
        {
            List<SqlParameter> paramters = new List<SqlParameter>();
            paramters.Add(new SqlParameter("@FirstName", customer.FirstName));
            paramters.Add(new SqlParameter("@LastName", customer.LastName));
            paramters.Add(new SqlParameter("@Email", customer.Email));
            paramters.Add(new SqlParameter("@Phone", customer.Phone));
            paramters.Add(new SqlParameter("@Gender", customer.Gender));
            paramters.Add(new SqlParameter("@HometownAddress", customer.HometownAddress));
            paramters.Add(new SqlParameter("@CurrentAddress", customer.CurrentAddress));
            paramters.Add(new SqlParameter("@SocialID", customer.SocialID));
            paramters.Add(new SqlParameter("@DateOfBirth", customer.DateOfBirth));
            paramters.Add(new SqlParameter("@CustomerID", customer.CustomerID));

            SqlHelper.ExecuteNonQuery("Update Customer set FirstName=@FirstName, LastName=@LastName, Email=@Email, " +
                "Phone=@Phone, Gender=@Gender, HometownAddress=@HometownAddress, CurrentAddress=@CurrentAddress," +
                "SocialID=@SocialID, DateOfBirth=@DateOfBirth WHERE CustomerID=@CustomerID", paramters);
        }
        public static void InsertCustomer(eCustomer customer)
        {
            List<SqlParameter> paramters = new List<SqlParameter>();
            paramters.Add(new SqlParameter("@FirstName", customer.FirstName));
            paramters.Add(new SqlParameter("@LastName", customer.LastName));
            paramters.Add(new SqlParameter("@Email", customer.Email));
            paramters.Add(new SqlParameter("@Phone", customer.Phone));
            paramters.Add(new SqlParameter("@Gender", customer.Gender));
            paramters.Add(new SqlParameter("@HometownAddress", customer.HometownAddress));
            paramters.Add(new SqlParameter("@CurrentAddress", customer.CurrentAddress));
            paramters.Add(new SqlParameter("@SocialID", customer.SocialID));
            paramters.Add(new SqlParameter("@DateOfBirth", customer.DateOfBirth));
            paramters.Add(new SqlParameter("@CustomerID", customer.CustomerID));

            SqlHelper.ExecuteNonQuery("Insert into Customer values(@FirstName, @LastName," +
                "@Gender, @DateOfBirth,@SocialID, @HometownAddress, @CurrentAddress, @Phone,@Email)", paramters);
        }
        public static void DeleteCustomerByID(int CustomerID)
        {
            SqlHelper.ExecuteNonQuery("Delete from Customer where CustomerID=" + CustomerID);
        }
    }
}
